package com.yh.controller.api;

import com.yh.anno.LoginAuthentication;
import com.yh.service.CommentService;
import com.yh.utils.Constants;
import com.yh.vo.CustomPageAndNavigate;
import com.yh.vo.CommentVO;
import com.yh.vo.ResponseVO;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author yuhuan
 * @date 2022/01/13
 */
@RestController
@RequestMapping("/api/comment")
public class CommentWeb {
    @DubboReference(check = false)
    private CommentService commentService;

    /**
     * 分页查找所有的影评详情
     * 服务地址 http://localhost:8080/api/comment/find/all/page
     */
    @RequestMapping("/find/all/page")
    public ResponseVO findCommentAllToPage(Integer movieId, Integer pageNum, Integer pageSize) {
        CustomPageAndNavigate page = commentService.initCommentVoListToPage(movieId, pageNum, pageSize);
        return ObjectUtils.isEmpty(page) ? ResponseVO.buildSuccessInstance(Constants.ResponseDataState.NULL_RESULT)
                : ResponseVO.buildSuccessInstance(page);
    }

    /**
     * 根据用户和影片查询影评信息
     * 服务地址 http://localhost:8080/api/comment/find/bum
     */
    @RequestMapping("/find/bum")
    @LoginAuthentication
    public ResponseVO findCommentByUserAndMovie(@RequestBody CommentVO commentVO) {
        commentVO = commentService.findCommentByUserAndMovie(commentVO);
        if (ObjectUtils.isEmpty(commentVO)) {
            return ResponseVO.buildSuccessInstance(Constants.ResponseDataState.NULL_RESULT);
        }
        return ResponseVO.buildSuccessInstance(commentVO);
    }

    /**
     * 首次添加影评（只会在没有评论过的情况下执行添加）
     * 服务地址 http://localhost:8080/api/comment/add
     */
    @RequestMapping("add")
    @LoginAuthentication
    public ResponseVO addCommentIfNotExist(@RequestBody CommentVO commentVO) {
        boolean result = commentService.addCommentIfNotExist(commentVO);
        return ResponseVO.buildSuccessInstance(Constants.ResponseDataState.RESP_SUCCESS);
    }

    /**
     * 添加或修改影评（首次评论就添加，否则一律执行修改操作）
     * 服务地址 http://localhost:8080/api/comment/add/update
     */
    @RequestMapping("/add/update")
    @LoginAuthentication
    public ResponseVO addOrUpdateComment(@RequestBody CommentVO commentVO) {
        boolean result = commentService.addOrUpdateComment(commentVO);
        return ResponseVO.buildSuccessInstance(Constants.ResponseDataState.RESP_SUCCESS);
    }
}
